package cn.dapeng.random;

import java.util.function.Supplier;

/**
 * @BelongProject study
 * @BelongPackage cn.dapeng.random
 * @Description 不等概率发生器，改写成等概率发生器
 * @ClassName RandomT01
 * @Author xupenghong
 * @Date 2024/8/20 14:36
 * @Version 1.0.0
 */
public class RandomT01 {

    public static int f1() {
        return (Math.random() > 0.84) ? 0 : 1;
    }

    public static void rate(Supplier<Integer> supplier) {
        int testTimes = 100_0000;
        int lessTimes = 0;
        int greaterTimes = 0;

        for (int i = 0; i < testTimes; i++) {
            int n = supplier.get();
            if (n == 0) {
                lessTimes++;
            } else {
                greaterTimes++;
            }
        }

        System.out.println(lessTimes);

        System.out.println(greaterTimes);

        System.out.println((double) lessTimes / (double) testTimes);
        System.out.println((double) greaterTimes / (double) testTimes);
    }

    public static int f2() {
        int ans;
        do {
            ans = (f1() << 1) + f1();
        } while (ans == 0 || ans == 3);
        return ans - 1;
    }

    public static int g() {
        int first;
        do {
            first = f1();
        } while (first == f1());
        return first;
    }

    public static void main(String[] args) {
        rate(RandomT01::f2);
    }
}
